package com.khadi.testcase.view.workflow;

import info.clearthought.layout.TableLayout;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.khadi.testcase.view.IWindowContent;
import com.khadi.testcase.view.model.ExecuteTestCaseModel;
import com.khadi.testcase.view.model.HtmlFieldModel;
import com.khadi.testcase.view.model.WorkflowDtlModel;

public class ExecuteTestCaseTab implements IWindowContent, ActionListener {

	private List<ExecuteTestCaseModel> list;

	@Override
	public Object getData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Component getView() {
		double[] columns = new double[] { -1 };
		double[] rows = new double[] { -2, -2, -1 };

		TableLayout executeTestCaseLayout = new TableLayout(columns, rows);
		executeTestCaseLayout.setHGap(5);
		executeTestCaseLayout.setVGap(5);

		JPanel executeTestCasePanel = new JPanel(executeTestCaseLayout);
		int i = 0;
		for (ExecuteTestCaseModel model : list) {
			executeTestCasePanel.add(createExecutePanel(model), "0," + (i++));
		}
		JScrollPane scrollPane = new JScrollPane(executeTestCasePanel);
		return scrollPane;
	}

	@Override
	public void postData(Object object) {

	}

	@Override
	public void preData(Object object) {
		if (object != null) {
			list = (LinkedList<ExecuteTestCaseModel>) object;
		}
	}

	private JPanel createExecutePanel(ExecuteTestCaseModel model) {
		double[] columns = new double[] { -2, -2, -2 };
		double[] rows = new double[] { -2 };

		TableLayout executeTestCaseLayout = new TableLayout(columns, rows);
		executeTestCaseLayout.setHGap(5);
		executeTestCaseLayout.setVGap(5);

		JPanel executePanel = new JPanel(executeTestCaseLayout);

		executePanel.setBorder(BorderFactory.createLoweredBevelBorder());

		JLabel lbTestCaseNo = new JLabel(model.getTestCaseModel()
				.getTestCaseNo());
		executePanel.add(lbTestCaseNo, "0,0");
		JCustBtn btnStart = new JCustBtn("Start", model);
		btnStart.addActionListener(this);
		executePanel.add(btnStart, "1,0");
		return executePanel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JCustBtn source = (JCustBtn) e.getSource();

		doExecuteTestCase(source.getExecuteTestCaseModel());

	}

	private void doExecuteTestCase(
			final ExecuteTestCaseModel executeTestCaseModel) {
		try {
			WebDriver driver = new FirefoxDriver();
			// And now use this to visit Google
			driver.get(executeTestCaseModel.getWorkFlowModel().getPageUrl());
			Thread.sleep(10000);
			final List<WorkflowDtlModel> workflowDtlList = executeTestCaseModel
					.getWorkFlowModel().getWorkflowDtlList();

			Map<HtmlFieldModel, WebElement> webElementMap = getWebElementMap(driver);

			for (WorkflowDtlModel workflowDtlModel : workflowDtlList) {

				WebElement webElement = webElementMap.get(workflowDtlModel
						.getHtmlFieldModel());

				if (workflowDtlModel.getCommandAction().equals("Click")) {

					webElement.click();
				} else {

					webElement.sendKeys(executeTestCaseModel.getTestCaseModel()
							.getPropertyMap().get(
									workflowDtlModel.getDescription()));

				}
			}
			Thread.sleep(10000);
			driver.quit();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private Map<HtmlFieldModel, WebElement> getWebElementMap(WebDriver driver) {
		Map<HtmlFieldModel, WebElement> webElementMap = new HashMap<HtmlFieldModel, WebElement>();

		List<WebElement> findElements = driver
				.findElements(By.tagName("input"));

		findElements.addAll(driver.findElements(By.tagName("button")));

		findElements.addAll(driver.findElements(By
				.className("z-toolbarbutton-cnt")));

		for (WebElement webElement : findElements) {

			String elementName = webElement.getAttribute("name");

			String elementType = webElement.getAttribute("type");

			String elementId = webElement.getAttribute("id");
			String className = webElement.getAttribute("className");

			String value = webElement.getText();

			HtmlFieldModel htmlFieldModel = new HtmlFieldModel(elementName,
					elementType, elementId, value, className);

			webElementMap.put(htmlFieldModel, webElement);
		}

		return webElementMap;
	}

	private class JCustBtn extends JButton {
		private ExecuteTestCaseModel model;

		public JCustBtn(String text, ExecuteTestCaseModel model) {
			super(text);
			this.model = model;

		}

		public ExecuteTestCaseModel getExecuteTestCaseModel() {
			return this.model;
		}
	}

}
